package com.bolt.admin.web.controller;


import com.bolt.admin.common.base.SimpleController;
import com.bolt.admin.properties.LoginProperties;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.support.cache.redis.RedisManager;
import com.wf.captcha.base.Captcha;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
@Slf4j
public class AdminController extends SimpleController {

    @Resource
    private LoginProperties loginProperties;

    @Autowired
    private RedisManager redisManager;



    @RequestMapping(value = "/admin/index", method = RequestMethod.GET)
    public String adminIndex() {
        return "/layouts/main_layout";
    }

    /**
     * @return
     */
    @RequestMapping(value = "/auth/login", method = RequestMethod.GET)
    public String login() {
        return "/admin/pub/login";
    }

    /**
     * 表单的/login POST请求首先会被Shiro拦截处理，在认证失败之后才会触发调用此方法
     *
     * @return
     */
    @RequestMapping(value = "/auth/login", method = RequestMethod.POST)
    public String login(HttpServletRequest request) {
        AuthenticationException ae =
                (AuthenticationException) request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
        if (ae != null) {
            request.setAttribute("error", ae.getMessage());
            return "/admin/pub/login";
        } else {
            return "/layouts/main_layout";
        }

    }

    /**
     * 打开首页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/admin/dashboard", method = RequestMethod.GET)
    public ModelAndView dashboard(HttpServletRequest request, Model model) {
        return super.index("/dashboard", request, model.asMap());

    }

    @ResponseBody
    @RequestMapping(value = "/captcha/graph")
    public ResultMessage <Map <String, Object>> captchaGraph() {
        Map <String, Object> resultMap = new HashMap <>(3);
        String uuid = UUID.randomUUID().toString();
        Captcha captcha = loginProperties.getCaptcha();
        resultMap.put("graphId", uuid);
        resultMap.put("img", captcha.toBase64());
        redisManager.cache(uuid, captcha.text(), loginProperties.getCaptchaCode().getExpiration(), TimeUnit.MINUTES);
        return Results.success(resultMap);
    }

}
